<!-- 定义模版对象 -->
<template>
    <div class="login_container">
      <div class="login_box">
        <!-- 头像区域-->
        <div class="avatar_box">
          <img src="../assets/logo.png" alt="VUE图片" />
        </div>

        <!-- 登陆表单区域
              ref代表当前表单引用对象
             :model 是表格中封装的对象
        -->
        <el-form ref="loginFormRef" label-width="0" class="login_form" :model="loginForm" :rules="rules" @submit.native.prevent>
          <el-form-item prop="username">
            <el-input  prefix-icon="iconfont iconuser" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input  prefix-icon="iconfont iconsuo" type="password" v-model.trim="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="warning" plain round @click="dialogVisible = true">注册</el-button>
              <el-button type="primary" :plain="true" @click="login" round native-type="submit">登录</el-button>
              <el-button type="info" @click="reset" round>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
<el-dialog title="注册用户" :visible.sync="dialogVisible" width="50%" @close="closeDialog">

      <!-- 定义用户提交表单数据-->
      <el-form :model="addUserModel" :rules="rules" ref="addUserRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserModel.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model.trim="addUserModel.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="密码确认" prop="password2">
          <el-input v-model.trim="addUserModel.password2" type="password"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addUserModel.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addUserModel.email"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false,addUserModel={}">取 消</el-button>
        <el-button type="primary" @click="addUserBtn">确 定</el-button>
      </span>
    </el-dialog>

    </div>
</template>

<!-- 定义JS变量 -->
<script>
export default {
  data(){
    //校验邮箱规则 rule校验规则   value校验的数据   callback回调函数
    const checkEmail = (rule, value, callback) => {
      //定义邮箱的正则表达式  JS中用/来表示正则表达式的开始和结束
      const emailRege = /^[a-zA-Z0-9-_]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+$/
      //正则表达式语法校验  test(xx) 校验成功 返回true  校验失败返回false
      if (emailRege.test(value)) {
        //表示邮箱合法 正确返回
        return callback()
      }
      callback(new Error('请填写正确的邮箱地址'))
    }

    //校验手机号的邮箱规则
    const checkPhone = (rule, value, callback) => {

      //定义校验手机号的正则语法
      const phoneRege = /^1[34578][0-9]{9}$/
      if (phoneRege.test (value)) {

        return callback()
      }
      callback(new Error('请填写正确的手机号'))

    }

    const checkPassword = (rule, value, callback) => {
      if(this.addUserModel.password !== value) return callback(new Error('2次密码输入不一致'))
      //否则校验成功
      callback()
    }
    return {

      dialogVisible: false,
      addUserModel: {
        username: '',
        password: '',
        password2: '',
        email:  '',
        phone:  ''
      },
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username:[
          /* required是否为必填项 message:提示信息 trigger触发条件*/
          {required: true,message: "请输入账号",trigger: 'blur'},
          {min: 3,max: 20,message: "请输入3~20位",trigger: 'blur'}
        ],
        password: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
              ],
        password2: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' },
                { validator: checkPassword , trigger: 'blur' }
              ],
        phone: [
                { required: true, message: '请输入手机号', trigger: 'blur' },
                { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' },
                { validator: checkPhone , trigger: 'blur' }
              ],
        email: [
                { required: true, message: '请输入邮箱', trigger: 'blur' },
                { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' },
                { validator: checkEmail , trigger: 'blur' }
              ]
      }
      //定义表单的验证规则
    }
  },
  methods: {
    closeDialog(){
      this.addUserModel = {}
    },
  addUserBtn(){
    this.$refs.addUserRef.validate(async valid => {
      //如果校验失败 则停止数据
      if(!valid) return
      //console.log(this.addUserModel)
      const {data: result} = await this.$http.post('/user/addUser',this.addUserModel)
      if(result.status !== 200) return this.$message.error("用户新增失败")
      this.$message.success("用户新增成功")
      //关闭对话框
      this.dialogVisible = false
      //重新获取用户列表

    })
  },
    reset(){
      /* this:代表当前的Vue的组件对象*/
      /* $refs $从vue对象中获取数据, 从vue组件对象中过去全部ref标签*/
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      this.$refs.loginFormRef.validate(async valid => {
        //当程序没有通过校验时 程序终止
        if(!valid) return
        //发起ajax的请求,实现业务调用
        //参数1: url地址 参数2:传递的数据
        let {data: result} = await this.$http.post("/user/login",this.loginForm);

        //4.判断用户校验是否正常 status=200 201
        if(result.status !== 200) return this.$message.error('用户名或密码错误');

        this.$message.success("登陆成功")
        //将我们用户的信息保存到session中
        window.sessionStorage.setItem("token",result.data)
        // window.sessionStorage.removeItem("token") 删除单个
        //window.sessionStorage.clear() 全部删除

        //5.登录成功后,跳转到/home界面中
        this.$router.push("/home")
      })
    }
  }
}
</script>

<!-- 防止组件冲突 -->
<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  background-image: url(../assets/img/首页背景图.jpg);
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #FFFFFF;
  /* 设定圆弧角*/
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #EEEEEE;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #DDDDDD; /* 添加阴影*/
    position: absolute;           /* 绝对定位*/
    left: 50%;                    /* 距离左侧50%*/
    transform: translate(-50%,-50%);   /*回调50%*/
    background-color: #FFFFFF;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #EEEEEE;
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;

  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
